﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services.Protocols;
using NLBS.Web.HttpHandlers;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using NLBS.Web;
using NLBS.Entity;
using System.Data.Common;
using System.Text;
using NLBS.Data;
using NLBS.Security;
using System.Web.Configuration;
using NLBS.Utility;
namespace NLBS.Blog.Services
{

    public class InstallSvc : AjaxHandler
    {

        public string TestCollection(string dataSource,string dataName,string userName,string password)
        {
            string connStr = string.Format("Data Source={0};Initial Catalog={1};uid={2};password={3}",
                dataSource,dataName,userName,password);
            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
            }
            catch (Exception ee)
            {
                
                throw ee;
            }
            return string.Empty;
        }

        public string InstallDataBase(string dataSource, string dataName, string userName, string password)
        {
            string connStr = string.Format("Data Source={0};Initial Catalog={1};uid={2};password={3}",
                dataSource, dataName, userName, password);
            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
                Configuration cfg = WebConfigurationManager.OpenWebConfiguration("/");
                ConnectionStringSettings setting = new ConnectionStringSettings();
                foreach (ConnectionStringSettings item in cfg.ConnectionStrings.ConnectionStrings)
                {
                    if (item.Name == "NLBSConnectionString")
                    {
                        setting = item;
                    }
                }
                setting.ConnectionString = connStr;
                cfg.Save();
                
            }
            catch (Exception ee)
            {
                throw ee;
            }
            return string.Empty;

        }

        public void RunSQL(string conn)
        {
            string sql1 = HttpContext.Current.Server.MapPath("Install/sqlServer/Installation01.sql");
            string sql2 = HttpContext.Current.Server.MapPath("Install/sqlServer/Installation02.sql");
            string sql3 = HttpContext.Current.Server.MapPath("Install/sqlServer/Installation03.sql");
            conn = @"Data Source=CLINGINGBOY-PC\SQLEXPRESS;Initial Catalog=demotest;Integrated Security=SSPI";
            int timeout = 600;
            NLBS.Utility.SqlHelper.RunSQL(sql1, conn, timeout);
            NLBS.Utility.SqlHelper.RunSQL(sql2, conn, timeout);
            NLBS.Utility.SqlHelper.RunSQL(sql3, conn, timeout);
        }

        public string InitBlog(string adminName, string adminpassword, string blogTitle,string blogDescription,string baseURL)
        {
            User user = new User();
            user.UserName = adminName;
            user.Password = StringSecurity.StringToSHA1Hash(adminpassword);
            user.Homepage = string.Empty;
            user.Salt = "";
            user.IP = RequestWrapper.IP;
            user.Gender = 0;
            user.GroupID = 1;
            user.HideEmail = true;
            user.Email = "nlbs@nlbs.com";
            user.LastVisit = DateTime.Now;
            DbProvider.Instance().InsertUser(user);
            Database database = DatabaseFactory.CreateDatabase();
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("update blog_settings set set_value1='{0}' where set_Name='blogTitle';", blogTitle);
            sb.AppendFormat("update blog_settings set set_value1='{0}' where set_Name='blogDescription';", blogDescription);
            sb.AppendFormat("update blog_settings set set_value1='{0}' where set_Name='baseURL';", baseURL);
            string a = "update blog_settings set set_value1={0} where set_Name='baseURL';";
            DbCommand dbCommand = database.GetSqlStringCommand(sb.ToString());
            database.ExecuteNonQuery(dbCommand);
            return string.Empty;
        }
    }
}
